/* eslint-disable */
export function formatDate(date, fmt) {
    fmt = "yyyy-MM-dd hh:mm:ss";
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
  };
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = `${o[k]}`;
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}

export function formatDate1(date, fmt) {
  fmt = "yyyy-MM-dd";
if (/(y+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
}
const o = {
  'M+': date.getMonth() + 1,
  'd+': date.getDate(),
  'h+': date.getHours(),
  'm+': date.getMinutes(),
  's+': date.getSeconds(),
};
for (const k in o) {
  if (new RegExp(`(${k})`).test(fmt)) {
    const str = `${o[k]}`;
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
  }
}
return fmt;
}

function padLeftZero(str) {
  return (`00${str}`).substr(str.length);
}

/**
 *获取某年某月某天是第几周
 *第一天为周日
 */
export function getWeekNumber(time) {
  var targetDay = new Date(time);
  var year = targetDay.getFullYear();
  var month = targetDay.getMonth();
  var days = targetDay.getDate();
  //那一天是那一年中的第多少天
  for (var i = 1; i < month+1; i++) {
      days += getMonthDays(year, i);
  }
  //那一年第一天是星期几
  var yearFirstDay = new Date(year, 0, 1).getDay();
  //计算是第几周
  days += yearFirstDay;
  var week = Math.ceil(days / 7);
  return week;
}

/**
 *获取某年某月的天数
 */
function getMonthDays(year, month) {
  return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
}
/**
 *判断年份是否为润年
 */
function isLeapYear(year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
